package com.example.administrator.yanfoxconn.activity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.yanfoxconn.R;
import com.example.administrator.yanfoxconn.constant.Constants;
import com.example.administrator.yanfoxconn.constant.FoxContext;
import com.example.administrator.yanfoxconn.utils.BaseActivity;
import com.example.administrator.yanfoxconn.utils.DateTimePickDialog30Util;
import com.example.administrator.yanfoxconn.utils.HttpConnectionUtil;
import com.example.administrator.yanfoxconn.utils.ToastUtils;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @Description 多媒體設備管理 取用
 * @Author song
 * @Date 11/15/21 10:58 AM
 */
public class KMDmtOutActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.btn_title_left)
    Button btnBack;//返回
    @BindView(R.id.tv_title)
    TextView tvTittle;//標題
    @BindView(R.id.btn_out_list)
    Button btnOutList;
    @BindView(R.id.iv)
    ImageView iv;//標題
    private String result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_km_dmt_out);
        ButterKnife.bind(this);
        tvTittle.setText("取用");
        btnBack.setOnClickListener(this);
        btnOutList.setOnClickListener(this);
        result=getIntent().getStringExtra("result");
        outUp("N");
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.btn_title_left:
                    finish();
                break;
            case R.id.btn_out_list:
                Intent intent = new Intent(KMDmtOutActivity.this,KMDmtListActivity.class);
                intent.putExtra("from","out");
                intent.putExtra("flag","out");
                startActivity(intent);
                break;
        }
    }
    Handler mHandler = new Handler() {
        @SuppressLint("HandlerLeak")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Constants.MESSAGE_TOAST://Toast彈出
                    ToastUtils.showLong(KMDmtOutActivity.this, msg.obj.toString());
                    iv.setVisibility(View.VISIBLE);
                    break;
                case Constants.MESSAGE_UP://t提交提示
                    ToastUtils.showLong(KMDmtOutActivity.this, msg.obj.toString());
                    finish();
                    break;
                case Constants.MESSAGE_NOT_NET://t提交提示
                    ToastUtils.showLong(KMDmtOutActivity.this, R.string.net_mistake);
                    break;
                    case Constants.MESSAGE_ALERT:
                        aboutAlert( msg.obj.toString(), 1);
                        break;
            }
            super.handleMessage(msg);
        }
    };
    //提交前檢查
    private void outUp(String flag) {
        final String url = Constants.HTTP_DMT_OUT_UP; //此處寫上自己的URL

        JsonObject object = new JsonObject();

        object.addProperty("a_out_people", FoxContext.getInstance().getName());
        object.addProperty("m_id", result);
        object.addProperty("flag", flag);


        //開啟一個新執行緒，向伺服器上傳資料
        new Thread() {
            public void run() {
                //把网络访问的代码放在这里
                try {
                    showDialog();
                    Log.e("---------", "==fff===" + url);
                    String result = HttpConnectionUtil.doPostJsonObject(url, object);
                    if (result != null) {
                        dismissDialog();
                        Log.e("---------", "result==fff===" + result);
                        JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                        String errCode = jsonObject.get("errCode").getAsString();
                        if (errCode.equals("200")) {
                            Message message = new Message();
                            message.what = Constants.MESSAGE_TOAST;
                            message.obj = jsonObject.get("errMessage").getAsString();
                            mHandler.sendMessage(message);

                        } else if (errCode.equals("404")){
                            Log.e("-----------", "result==" + result);
                            Message message = new Message();
                            message.what = Constants.MESSAGE_ALERT;
                            message.obj = jsonObject.get("errMessage").getAsString();
                            mHandler.sendMessage(message);
                        }else{

                            Log.e("-----------", "result==" + result);
                            Message message = new Message();
                            message.what = Constants.MESSAGE_UP;
                            message.obj = jsonObject.get("errMessage").getAsString();
                            mHandler.sendMessage(message);
                        }
                    } else {
                        Message message = new Message();
                        message.what = Constants.MESSAGE_NOT_NET;
                        mHandler.sendMessage(message);
                        finish();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private void aboutAlert(String msg, final int type) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("優先取用")
                .setMessage(msg)
                .setCancelable(false)
                .setPositiveButton("取用", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        // TODO Auto-generated method stub
                        if (type==1){
                            outUp("Y");
                        }
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        // TODO Auto-generated method stub
//                        if (type==MESSAGE_TOAST){
//                            finish();
//                        }else if(type==MESSAGE_UP){
//                            check();
//                        }
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
    }

}
